package com.ecity.table {
	import com.ecity.util.HashMap;
	import com.ecity.util.UIFactory;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import gs.easing.*;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	public class TableDisplayObject extends Sprite {
		private var topLayer:Sprite;
		private var _layerMap:HashMap;
		private var angle:int=0;
		private var isPrompt:Boolean;
		private var bg:DisplayObject;
		
		//public var 
		
		public function TableDisplayObject() {
			_layerMap = new HashMap();
			isPrompt = true;
		}
		
		protected function clearLayer(layer:Sprite):void {
			while (layer.numChildren) {
				layer.removeChildAt(0);
			}
		}
		
		protected function getLayer(name:String):Sprite {
			var layer:Sprite;
			if (_layerMap.containsKey(name)) {
				layer = _layerMap.get(name);
			}else {
				layer  = new Sprite();
				layer.name = "layer_" + name;
				_layerMap.put(name, layer);
				this.addChild(layer);
			}
			return layer;
		}
		
		protected function removeLayer(name:String):void {
			var layer:Sprite;
			if (_layerMap.containsKey(name)) {
				layer = _layerMap.get(name);
				this.removeChild(layer);
				_layerMap.remove(name);
			}
		}
		
		public function addChildAtBottom(child:DisplayObject):void {
			this.addChildAt(child, 0);
		}
		
		public function addChildAs(child:DisplayObject):void {
			this.addChildAt(child, this.getChildIndex(topLayer));
		}
		
		public function addChildAtTop(child:DisplayObject):void {
			topLayer = this.getLayer("the_top_layer");
			topLayer.addChild(child);
		}
		
		public function initChildren(classkey:String):void {
			var cxml:XML = UIFactory.getConfiger();
			for each(var item:XML in cxml.layout[classkey].children()){
				var key:String = item.@id;
				this.layout(key,classkey,cxml);
			}
		}
		
		private function layout(key:String, classKey:String, cxml:XML):void {
			var cxml:XML = UIFactory.getConfiger();
			var layer:Sprite = this.getLayer(key);
			var rbmp:Bitmap = UIFactory.getBitmapbyName(key) as Bitmap;
			var bmd:BitmapData = rbmp.bitmapData.clone();
			var bmp:Bitmap = new Bitmap(bmd);
			bmp.x = bmp.width * -0.5 + Number(cxml.layout[classKey].ui.(@id==key).@x);
			bmp.y = bmp.height * -0.5 + Number(cxml.layout[classKey].ui.(@id == key).@y);
			if (classKey!="player"||classKey!="poker") {
				layer.x =1024/ 2;
				layer.y = 768 / 2;
			}
			layer.alpha = cxml.layout[classKey].ui.(@id==key).@alpha;
			layer.scaleX = cxml.layout[classKey].ui.(@id==key).@scale;
			layer.scaleY= cxml.layout[classKey].ui.(@id==key).@scale;
			layer.addChild(bmp);
		}

		
	}

}